#include "qboolproperty.h"

#include <QStyleOptionButton>
#include <QPixmap>
#include <QApplication>
#include <QPainter>


QBoolProperty::QBoolProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","Bool");
}

QString QBoolProperty::get_value_text()
{
    return get_value().toString();
}

QIcon QBoolProperty::get_value_icon()
{
    QStyleOptionButton opt;
    opt.state |=get_value().toBool()?QStyle::State_On:QStyle::State_Off;

    opt.state |=QStyle::State_Enabled;

    const QStyle* style=QApplication::style();
    const int indicatiorWidth=style->pixelMetric(QStyle::PM_IndicatorWidth,&opt);
    const int indicatiorHeight=style->pixelMetric(QStyle::PM_IndicatorHeight,&opt);
    const int listViewIconSize=indicatiorWidth;
    const int pixmapWidth=indicatiorWidth;
    const int pixmapHeight=qMax(indicatiorHeight,listViewIconSize);

    opt.rect=QRect(0,0,indicatiorWidth,indicatiorHeight);

    QPixmap pixmap=QPixmap(pixmapWidth,pixmapHeight);

    pixmap.fill(Qt::transparent);

    {
        const int xoff=(pixmapWidth>indicatiorWidth)?(pixmapWidth-indicatiorWidth)/2:0;
        const int yoff=(pixmapHeight>indicatiorHeight)?(pixmapHeight-indicatiorHeight)/2:0;
        QPainter p(&pixmap);
        p.translate(xoff,yoff);
        style->drawPrimitive(QStyle::PE_IndicatorCheckBox,&opt,&p);
    }
    return QIcon(pixmap);
}

qRegisterProperty("Bool",QBoolProperty)
